package SmartHome.dao.impl;

import SmartHome.dao.UserDao;
import SmartHome.entity.UserInfo;
import SmartHome.util.JdbcUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName : UserDaoImpl
 * Description :
 * Author : Wang Beijia
 * Date: 2021-07-06 10:09
 */
public class UserDaoImpl implements UserDao{

    public int executeUpdate(String sql, Object... params) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            // 1.加载驱动
            // 2.连接数据库
            conn = JdbcUtil.getConn();
            // 3.创建语句
            ps = conn.prepareStatement(sql);
            // 遍历参数
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            // 4.执行语句
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 5.释放资源
            JdbcUtil.close(conn, ps, null);
        }
        return 0;
    }

    @Override
    public void add(UserInfo user) {
        String sql = "insert into UserInfo(userID,userName,UserPassword,UserPhoneNumber,FId,uState) values (?,?,?,?,?,1)";
        this.executeUpdate(sql, user.getUserId(), user.getUserName(), user.getPassword(), user.getUserPhoneNumber(), user.getFId());
    }

    @Override
    public void delete(int uId) {
        String sql = "update Userinfo set uState=0 where userId=?";
        this.executeUpdate(sql, uId);
    }

    @Override
    public UserInfo query(int uId) {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            String sql = "select * from userinfo where uState=1 and userId ="+uId;
            conn = JdbcUtil.getConn();
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            if (rs.next()) {
                UserInfo user = new UserInfo(rs.getInt(1),rs.getString("userName"),
                        rs.getString("UserPassword"), rs.getString("UserPhoneNumber"),
                        rs.getInt("FId"),1);
                return user;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 5.释放资源
            JdbcUtil.close(conn, st, rs);
        }
        return null;
    }

    @Override
    public void update(int uId, UserInfo user) {
        String sql = "update UserInfo set userId=?, userName=?, UserPassword=?, UserPhoneNumber=? where userId =? ";
        this.executeUpdate(sql, user.getUserId(), user.getUserName(), user.getPassword(),
                user.getUserPhoneNumber(), uId);
    }

    public List<UserInfo> getAll() {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConn();
            st = conn.createStatement();
            String sql = "select * from userinfo where uState=1";
            System.out.println(sql);
            rs = st.executeQuery(sql);
            List<UserInfo> list = new ArrayList<UserInfo>();

            while(rs.next()) {
                UserInfo user = new UserInfo(rs.getInt(1), rs.getString("userName"),
                        rs.getString("UserPassword"), rs.getString("UserPhoneNumber"),
                        rs.getInt("FId"),1);
                list.add(user);
            }
            System.out.println(list);
            return list;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, st, rs);
        }
        return null;
    }
}

